/*
 * Copyright 2004-2006 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.sysframe.valid;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.asoft.common.sysframe.manager.OrgManager;
import com.asoft.common.sysframe.manager.PostManager;
import com.asoft.common.sysframe.manager.RoleManager;
import com.asoft.common.sysframe.model.Org;
import com.asoft.common.sysframe.model.Post;
import com.asoft.common.sysframe.model.Role;
import com.asoft.common.util.mvc.valid.UserValidator;
import com.asoft.common.util.mvc.valid.ValidatedResult;

/**
 * <p>Title: 岗位</p>
 * <p>Description: 岗位 Role </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: asoft</p>
 * @ $Author: amon.lei $
 * @ $Date: 2007-2-20 $
 * @ $Revision: 1.0 $
 * @ created in 2007-2-20
 *
 */

public class AddPostsValidator extends UserValidator
{
        static Logger logger = Logger.getLogger(AddPostsValidator.class);

        /**
         * 新增岗位
         *
         * @ param: HttpServletRequest request
         * @ param: ValidatedResult vr 校验结果存放器
         * @ param: String[] params 配置参数
         */

        public void validing(HttpServletRequest request,ValidatedResult vr,String[] params)
        {
                logger.debug("开始进行新增岗位前校验");
                PostManager postManager = (PostManager)this.getBean("postManager");
                OrgManager orgManager = (OrgManager)this.getBean("orgManager");
                RoleManager roleManager = (RoleManager)this.getBean("roleManager");
                String[] orgIds = request.getParameterValues("orgIds");
                if(orgIds == null)
                {
                        vr.setErrMess("orgIds","请指定");
                }
                else
                {
                        if(orgIds.length == 0)
                        {
                                vr.setErrMess("orgIds","请指定");
                        }
                        logger.debug("orgids.length(): " + orgIds.length);
                }
                
                String[] roleIds = request.getParameterValues("roleIds");
                if(roleIds == null)
                {
                        vr.setErrMess("roleIds","请指定");
                }
                else
                {
                        if(roleIds.length == 0)
                        {
                                vr.setErrMess("roleIds","请指定");
                        }
                        logger.debug("roleIds.length(): " + roleIds.length);
                }
                
                int orgLength = this.validInt(request,vr,"orgLength",true);
                if(vr.isValidated("orgLength"))
                {
                        if(orgLength < 0)
                        {
                                vr.setErrMess("orgLength","必须大于0");
                        }
                }
                logger.debug("orgLength: " + orgLength);
                
                int roleLength = this.validInt(request,vr,"roleLength",true);
                if(vr.isValidated("roleLength"))
                {
                        if(roleLength < 0)
                        {
                                vr.setErrMess("roleLength","必须大于0");
                        }
                }
                logger.debug("roleLength: " + roleLength);
                
                if(vr.isAllValidated())
                {
                        logger.debug("通过校验,开始保存");
                        List posts = new ArrayList();
                        for(int i = 0; i < orgIds.length; i ++)
                        {
                                for(int j = 0; j < roleIds.length; j ++)
                                {
                                        if(postManager.getByOrgIdNRoleId(orgIds[i],roleIds[j]) == null)
                                        {
                                                Post post = new Post();
                                                post.setOrg((Org)orgManager.get(orgIds[i]));
                                                post.setRole((Role)roleManager.get(roleIds[j]));
                                                post.setOrgLength(orgLength);
                                                post.setRoleLength(roleLength);
                                                posts.add(post);
                                        }
                                }
                        }
                        logger.debug("共有" + posts.size() + "个新岗位");
                        request.setAttribute(Post.class.getName(),posts);
                }
                
        }
}
